package com.ynu.controller;

import com.ynu.bean.PageBean;
import com.ynu.bean.query.GoodsQuery;
import com.ynu.bean.vo.AdvertiseVO;
import com.ynu.bean.vo.CaraselVO;
import com.ynu.bean.vo.CategoryVO;
import com.ynu.bean.vo.GoodsVO;
import com.ynu.constant.WeGo;
import com.ynu.service.AdvertiseService;
import com.ynu.service.CaraselService;
import com.ynu.service.CategoryService;
import com.ynu.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 *  模块名称：
 *  模块类型：
 *  编码人：迟宽席
 *  创建时间：2023/3/1
 *  联系电话：18388068273
 */
@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private CaraselService caraselService;

    @Resource
    private AdvertiseService advertiseService;

    @Resource
    private GoodsService goodsService;


    @GetMapping("/index")
    public ModelAndView index(ModelAndView mav) {
        mav.setViewName("frontend/index");

        //查找到需要显示的顶级类别
        List<CategoryVO> topCategoryVOList = categoryService.getTopCategory(WeGo.TOP_CATEGORY_AMOUNT);
        for (CategoryVO topCategoryVO : topCategoryVOList) {
            List<CategoryVO> secondCategoryVOList = categoryService.getCategoryByPid(topCategoryVO.getId(),1, WeGo.SECOND_CATEGORY_AMOUNT);
            topCategoryVO.setSub(secondCategoryVOList);
        }
        mav.addObject("categoryList",topCategoryVOList);

        //轮播图
        List<CaraselVO> caraselVOList = caraselService.getCarasel(5);
        mav.addObject("caraselList",caraselVOList);
        System.out.println(caraselVOList);

        //水平广告

        List<AdvertiseVO> horizontalAdvertiseVOList =  advertiseService.getAdvertise(1,1,3);
        mav.addObject("horizontalAdvertiseList",horizontalAdvertiseVOList);

        //垂直广告
        List<AdvertiseVO> verticalAdvertiseVOList =   advertiseService.getAdvertise(2,1,4);
        mav.addObject("verticalAdvertiseList",verticalAdvertiseVOList);

        //促销
        //推荐商品
        GoodsQuery recommendGoodsQuery = new GoodsQuery();
        recommendGoodsQuery.setPageNum(1);
        recommendGoodsQuery.setPageSize(5);
        //可用
        recommendGoodsQuery.setState(1);
        recommendGoodsQuery.setRecommend(true);
        PageBean<GoodsVO> recommendGoodsPageBean = goodsService.getGoodsByConditionWithPage(recommendGoodsQuery);
        List<GoodsVO> recommendGoodsList = recommendGoodsPageBean.getResult();
        mav.addObject("recommendGoodsList",recommendGoodsList);
        //促销商品
        GoodsQuery promotionGoodsQuery = new GoodsQuery();
        promotionGoodsQuery.setPageNum(1);
        promotionGoodsQuery.setPageSize(5);
        //可用
        promotionGoodsQuery.setState(1);
        promotionGoodsQuery.setPromotion(true);
        PageBean<GoodsVO> promotionGoodsPageBean = goodsService.getGoodsByConditionWithPage(promotionGoodsQuery);
        List<GoodsVO> promotionGoodsList = promotionGoodsPageBean.getResult();
        mav.addObject("promotionGoodsList",promotionGoodsList);


        //查询指定父类别（前3个）下的所有的子类对应的商品（此处代码只考虑功能实现，后期可以优化）
        List<List<GoodsVO>> goodsList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            CategoryVO topCategoryVO = topCategoryVOList.get(i);
            List<CategoryVO> subList = topCategoryVO.getSub();
            List<Long> subIds = new ArrayList<>();
            for (CategoryVO subCategoryVO : subList) {
                subIds.add(subCategoryVO.getId());
            }
            System.out.println(subIds);
            GoodsQuery goodsQuery = new GoodsQuery();
            goodsQuery.setPageNum(1);
            goodsQuery.setPageSize(8);
            goodsQuery.setState(1);
            goodsQuery.setCategoryIds(subIds);
            PageBean<GoodsVO> goodsVOPageBean = goodsService.getGoodsByConditionWithPage(goodsQuery);
            List<GoodsVO> goodsVOList = goodsVOPageBean.getResult();
            goodsList.add(goodsVOList);
        }
        mav.addObject("goodsList",goodsList);


        return mav;
    }



}
